import java.io.BufferedReader;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.HashMap;

public class Bigram {
	// Bigram Table
	public HashMap<String, Integer> maptable;

	public static void main(String[] args) {
		Bigram b = new Bigram("Enrontest.txt");
		// System.out.println(b.getfreqS("hello"));
	}

	public Bigram(String file) {
		maptable = new HashMap<String, Integer>();
		// table = new ArrayList<ArrayList<Integer>>();
		makeTable(file);

	}

	private void makeTable(String file) {
		// ArrayList<String> namelist = initializeTable(file);
		try {
			String filename = file;
			FileReader fread = new FileReader(filename);
			BufferedReader br = new BufferedReader(fread);
			String strLine;
			String endOfLastLine = null;
			// Read File Line By Line
			while ((strLine = br.readLine()) != null) {
				String[] array = strLine.split("\\s+");
				ArrayList<String> targetlist = new ArrayList<String>();
				for (int i = 0; i < array.length; i++) {
					targetlist.addAll(breakUpPunctuation(array[i]));
				}
				// Handle spill over from previous line, if necessary
				if (endOfLastLine != null && targetlist.size() > 0) {
					String firstword = endOfLastLine.toLowerCase();
					String secondword = targetlist.get(0).toLowerCase();
					Integer freq = maptable.get(firstword + " " + secondword);
					if (freq != null) {
						maptable.put(firstword + " " + secondword, freq + 1);
					} else {
						maptable.put(firstword + " " + secondword, 1);
					}
				}
				for (int i = 1; i < targetlist.size(); i++) {
					String firstword = targetlist.get(i - 1).toLowerCase();
					String secondword = targetlist.get(i).toLowerCase();
					Integer freq = maptable.get(firstword + " " + secondword);
					if (freq != null) {
						maptable.put(firstword + " " + secondword, freq + 1);
					} else {
						maptable.put(firstword + " " + secondword, 1);
					}
				}
				endOfLastLine = targetlist.size() > 0 ? targetlist.get(targetlist.size() - 1) : null;
			}
			// Close the input stream
			br.close();
		} catch (Exception e) {// Catch exception if any
			e.printStackTrace();
		}
	}

	private float probability(int tot, int found) {
		float ftot = tot;
		float ffound = found;
		return (ffound / ftot);
	}

	private static ArrayList<String> breakUpPunctuation(String target) {
		ArrayList<String> output = new ArrayList<String>();
		String prev = "";
		// We want to avoid treating HTML tags as words
		if (target.length() > 0 && target.charAt(0) == '<') {
			return output;
		}
		for (int ptr = 0; ptr < target.length(); ptr++) {
			if (target.charAt(ptr) == '.' || target.charAt(ptr) == '!' || target.charAt(ptr) == '?'
					|| target.charAt(ptr) == ':' || target.charAt(ptr) == ';') {
				if (prev.length() > 0) {
					output.add(prev);
					prev = "";
				}
				output.add(target.substring(ptr, ptr + 1));
			} else if (target.charAt(ptr) != '(' && target.charAt(ptr) != ')' && target.charAt(ptr) != '\''
					&& target.charAt(ptr) != '\"' && target.charAt(ptr) != ',') {
				prev = prev + target.substring(ptr, ptr + 1);
			}
		}
		if (prev.length() > 0) {
			output.add(prev);
			prev = "";
		}
		return output;
	}

}
